﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Sql;
using DevExpress.Web.ASPxGridView;
public partial class HakedisHareket : System.Web.UI.Page
{

    public string ay, yil,personelKodu;
    private DcSql conn;
    private string VT_Prim = "Prim";  
    
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new DcSql();
        string hataMesaji = conn.DBBaglan(VT_Prim);

        string selectSql = "select cari_per_kod,cari_per_adi from mikroDB.dbo.CARI_PERSONEL_TANIMLARI order by cari_per_adi ";

        SqlDataAdapter da = new SqlDataAdapter(selectSql, conn.GetConn());

        DataSet ds = new DataSet();
        da.Fill(ds, "Personel");

        cmbPersonel.DataSource = ds.Tables[0];
        cmbPersonel.TextField = "cari_per_adi";
        cmbPersonel.ValueField = "cari_per_kod";
        cmbPersonel.DataBind();

        personelKodu = "";
    }

    
    protected void btnHesapla_Click1(object sender, EventArgs e)
    {

        Yenile();
    }

    private void Yenile()
    {
        string personelSql = "";
        Session["yil"] =  ASPxSpinEdit1.Value.ToString();
        Session["ay"] = (cmbAy.SelectedIndex + 1).ToString();
        Session["personelKodu"]  = cmbPersonel.Value.ToString();

        ASPxLabel5.Text = cmbPersonel.Text.ToString() + "  " + cmbAy.Text.ToString() + "  " + Session["yil"].ToString() + " PRİM BONUS HAREKET ";

        if (cmbPersonel.Value.ToString().Length > 0)
            personelSql = " and sPersonelKodu ='" + cmbPersonel.Value.ToString() + "'";

        string selectsql = " select * from HakedisHareket " +
                            "  where nPrimBonusYil=" + Session["yil"].ToString() + " and nPrimBonusAy=" + Session["ay"].ToString();
                            //+ personelSql;



        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(selectsql, conn.GetConn());

        da.Fill(ds, "Hakedis");

        gridListe.Visible = true;
        gridListe.DataSource = ds.Tables[0];
        gridListe.DataBind();

        if (!IsPostBack && !IsCallback)
            gridListe.DataBind();
    }
    protected void gridListe_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        SqlCommand cmd = new SqlCommand("HakedisHareketEkle", conn.GetConn());
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@sPersonelKodu", Session["personelKodu"].ToString());
        cmd.Parameters.AddWithValue("@nPrimBonusAy", Session["ay"].ToString());
        cmd.Parameters.AddWithValue("@nPrimBonusYil", Session["yil"].ToString());
        cmd.Parameters.AddWithValue("@nHareketTuru", e.NewValues["nHareketTuru"]);
        cmd.Parameters.AddWithValue("@dtHareketTarihi", e.NewValues["dtHareketTarihi"]);
        cmd.Parameters.AddWithValue("@sAciklama", e.NewValues["sAciklama"]);
        cmd.Parameters.AddWithValue("@nTutar", e.NewValues["nTutar"]);
   
        cmd.ExecuteScalar();

        e.Cancel = true;
        gridListe.CancelEdit();
        Yenile();
    }
    protected void gridListe_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        SqlCommand cmd = new SqlCommand("HakedisHareketDuzelt", conn.GetConn());
        cmd.CommandType = CommandType.StoredProcedure;

        ASPxGridView gridView = (ASPxGridView)sender;
        e.NewValues["HakedisHareketID"] = gridView.GetRowValues(gridView.EditingRowVisibleIndex, "HakedisHareketID").ToString();

        cmd.Parameters.AddWithValue("@HakedisHareketID", e.NewValues["HakedisHareketID"]);
        cmd.Parameters.AddWithValue("@sPersonelKodu", Session["personelKodu"].ToString());
        cmd.Parameters.AddWithValue("@nPrimBonusAy", Session["ay"].ToString());
        cmd.Parameters.AddWithValue("@nPrimBonusYil", Session["yil"].ToString());
        cmd.Parameters.AddWithValue("@nHareketTuru", e.NewValues["nHareketTuru"]);
        cmd.Parameters.AddWithValue("@dtHareketTarihi", e.NewValues["dtHareketTarihi"]);
        cmd.Parameters.AddWithValue("@sAciklama", e.NewValues["sAciklama"]);
        cmd.Parameters.AddWithValue("@nTutar", e.NewValues["nTutar"]);

        cmd.ExecuteScalar();

        e.Cancel = true;
        gridListe.CancelEdit();
        Yenile();
    }
    protected void gridListe_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        SqlCommand cmd = new SqlCommand("HakedisHareketSil", conn.GetConn());
        cmd.CommandType = CommandType.StoredProcedure;

        ASPxGridView gridView = (ASPxGridView)sender;
        int index = gridView.FindVisibleIndexByKeyValue(e.Keys[gridView.KeyFieldName]);

        string HakedisHareketID = gridView.GetRowValues(index, gridView.KeyFieldName).ToString();
        cmd.Parameters.AddWithValue("@HakedisHareketID", HakedisHareketID);
        cmd.ExecuteScalar();

        e.Cancel = true;
        gridView.CancelEdit();
        Yenile();
    }
}